package com.foreknow.elm.dao.Impl;


import com.foreknow.elm.dao.BusinessDao;
import com.foreknow.elm.po.Business;
import com.foreknow.elm.util.DBUtil;


import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class BusinessDaoImpl implements BusinessDao {
    private Connection con ;
    private PreparedStatement pst;
    private ResultSet rs;
    @Override
    public List<Business> listBusinessByOrderTypeId(Integer orderTypeId) throws SQLException {
        String sql = "select * from business where orderTypeId = ?  ORDER BY businessId";
        List<Business> list = new ArrayList<>();
        try {
            //Connection从ThreadLocal中获取
            Connection con = DBUtil.getConnection();
            pst = con.prepareStatement(sql);
            pst.setInt(1,orderTypeId);
            //执行查询，并将查询到的结果放到结果集
            //遍历结果集并将每行数据保存到List集合中
            rs = pst.executeQuery();
            while (rs.next()){
                Business business = new Business();
                business.setBusinessId(rs.getInt("businessId"));
                business.setBusinessName(rs.getString("businessName"));
                business.setBusinessAddress(rs.getString("businessAddress"));
                business.setBusinessExplain(rs.getString("businessExplain"));
                business.setBusinessImg(rs.getString("businessImg"));
                business.setOrderTypeId(rs.getInt("orderTypeId"));
                business.setStarPrice(rs.getDouble("starPrice"));
                business.setDeliveryPrice(rs.getDouble("deliveryPrice"));
                business.setRemarks(rs.getString("remarks"));
                list.add(business);
            }
        }finally {                          //这里不能处理异常，也就是没有catch，只有finally
            DBUtil.close(pst);              //这里负责关闭PreparedStatement和ResultSet
        }
        return list;
    }

    @Override
    public Business getBusinessById(Integer businessId) throws SQLException {
        Business business = null;
        String sql = "select * from business where businessId = ?";
        try {
            //Connection从ThreadLocal中获取
            Connection con = DBUtil.getConnection();
            pst = con.prepareStatement(sql);
            pst.setInt(1,businessId);
            //执行查询，并将查询到的结果放到结果集
            //遍历结果集并将每行数据保存到List集合中
            rs = pst.executeQuery();
            while (rs.next()){
                business = new Business();
                business.setBusinessId(rs.getInt("businessId"));
                business.setBusinessName(rs.getString("businessName"));
                business.setBusinessAddress(rs.getString("businessAddress"));
                business.setBusinessExplain(rs.getString("businessExplain"));
                business.setBusinessImg(rs.getString("businessImg"));
                business.setOrderTypeId(rs.getInt("orderTypeId"));
                business.setStarPrice(rs.getDouble("starPrice"));
                business.setDeliveryPrice(rs.getDouble("deliveryPrice"));
                business.setRemarks(rs.getString("remarks"));
            }
        }finally {                          //这里不能处理异常，也就是没有catch，只有finally
            DBUtil.close(pst);              //这里负责关闭PreparedStatement和ResultSet
        }
        return business;
    }
}